package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.younion.business.ICustomerService;
import com.younion.business.IShopService;
import com.younion.business.IUserService;
import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.util.UUIDUtil;
import com.younion.vo.CustomerVO;
import com.younion.vo.ShopVO;

/**
 * @author jillchan
*/
public class ShopAction extends BaseAction {

	private static final long serialVersionUID = 8555552032813973203L;
	
	protected static final Log logger = LogFactory.getLog(ShopAction.class);
	private IShopService shopService = null;

	
	private IUserService userService = null;
	
	private ICustomerService customerService = null;
	
	private ShopVO shopVO = new ShopVO();

	private List<ShopVO> shopList;// 结果集列表
	
	private UUIDUtil uUIDUtil = new UUIDUtil();
	
	private Integer templateId ;
	
	/**
	 *  列表：翻页区；数据区
	 */
	@RequiresPermissions("listShop")
	public String listShop() {
		try {
			Map paramMap = null;
			String customerId = request.getParameter("customerId");
			if (shopVO != null) {
				if(customerId != null){
					CustomerVO customer =  customerService.getById(customerId);
					shopVO.setCustomer(customer);
				}
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(shopVO));
			}
			// 获取总记录数
			int total = shopService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			shopList = shopService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	@RequiresPermissions("addShop")
	public String addShop() {
		
		try {
			String customerId = request.getParameter("customerId");
//			System.out.println("customerId="+customerId);
			CustomerVO customer = customerService.getById(customerId);
//			System.out.println("customer="+customer);
			request.setAttribute("customer", customer);
			String code = uUIDUtil.getUuid().toString();
			request.setAttribute("code", code);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	@RequiresPermissions("editShop")
	public String editShop() {
		try {
			//if super user login,provide all customer 
			//List customerList = (List) customerService.getAll();
			//otherwise provide its own customer
			shopVO = shopService.getById(shopVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	@RequiresPermissions("saveShop")
	public String saveShop() {
		try {
			String customerId = request.getParameter("customerId");
			CustomerVO customer = customerService.getById(customerId);
			shopVO.setCustomer(customer);
			shopVO = shopService.save(shopVO);
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("relDivId", "customerEditShopListTab");
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	@RequiresPermissions("deleteShop")
	public String deleteShop() {
		try {
			String pkId = request.getParameter("ids");
			shopService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		request.setAttribute("relDivId", "customerEditShopListTab");
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	@RequiresPermissions("viewShop")
	public String viewShop() {
		try {
			String pkId = request.getParameter("ids");
			shopVO = shopService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}
	

	public ShopVO getShopVO() {
		return shopVO;
	}

	public void setShopVO(ShopVO shopVO) {
		this.shopVO = shopVO;
	}

	public void setShopService(IShopService shopService) {
		this.shopService = shopService;
	}

	public List<ShopVO> getShopList() {
		return shopList;
	}

	public void setShopList(List<ShopVO> shopList) {
		this.shopList = shopList;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	
	public Integer getTemplateId() {
		return templateId;
	}

	public void setTemplateId(Integer templateId) {
		this.templateId = templateId;
	}
}